package _剑指offer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Liu Huan
 */

public class _57_和为s的连续正数序列 {
    public static void main(String[] args) {
        
        Long start = System.currentTimeMillis();
        int[][] result = findContinuousSequence(9);
//        int[][] result = findContinuousSequence(98160);
        Long end = System.currentTimeMillis();
        System.out.println("用时：" + (end-start) + " ms");
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[i].length; j++) {
                System.out.print(result[i][j] + "\t");
            }
            System.out.println();
        }
    }
    
    public static int[][] findContinuousSequence(int target) {
        List<int[]> result = new ArrayList<>();
        int mid = target/2+1;
        for(int i=1; i<mid; i++) {
            int sum = i;
            for(int j=i+1; ; j++) {
                if(sum > target) break;
                sum += j;
                if(sum == target) {
                    int[] temp = new int[j-i+1];
                    for (int k = 0; k < temp.length; k++) {
                        temp[k] = i+k;
                    }
                    result.add(temp);
                }
            }
        }
        return result.toArray(new int[result.size()][]);
    }
}
